package org.ishequ.adapter;

import java.util.List;

import org.ishequ.db.R;
import org.ishequ.domain.Person;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class PersonAdapter extends BaseAdapter {

	private List<Person> ldata;
	private int resource;
	private LayoutInflater inflater;
	
	public PersonAdapter(Context context,int resource,List<Person> persons){
		this.ldata = persons;
		this.resource = resource;
		this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	@Override
	public int getCount() {
		return ldata.size();
	}

	@Override
	public Object getItem(int position) {
		return ldata.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView == null){
			convertView = inflater.inflate(resource, null);
		}
		bindView(position,convertView);
		return convertView;
	}
	
	private void bindView(int position,View view){
		Person person = ldata.get(position);
		TextView name = (TextView)view.findViewById(R.id.name);
		name.setText(person.getName());
		TextView phone = (TextView)view.findViewById(R.id.phone);
		phone.setText(person.getPhone());
		TextView amount = (TextView)view.findViewById(R.id.amount);
		amount.setText(String.valueOf(person.getAmount()));
	}

}
